//
// Created by 33145 on 2023/6/9.
//
#include "../rocket/tcp/tcp_server.h"
#include <memory>
#include <google/protobuf/service.h>

#include "../rocket/common/rocket.h"
#include "order.pb.h"
#include "../rocket/rpc/rpc_dispatcher.h"

class OrderImp : public Order {
public:
  void makeOrder(google::protobuf::RpcController* controller,
                         const ::makeOrderRequest* request,
                         ::makeOrderResponse* response,
                         ::google::protobuf::Closure* done) override { // 业务逻辑
    LOG_INFO("sleep 5 second");
    sleep(5);
    if (request->price() < 10) {
      response->set_ret_code(-1);
      response->set_res_info("short balance");
      return;
    }
    response->set_order_id("20230609");
  }

};

void test_tcp_server() {
  rocket::IPNetAddr::ptr addr = std::make_shared<rocket::IPNetAddr>("127.0.0.1", 12345);
  LOG_DEBUG("create addr %s", addr->toString().c_str());
  rocket::TCPServer::ptr tcp_server = std::make_shared<rocket::TCPServer>(addr);
  tcp_server->start();
}

int main() {
  rocket::Config::SetGlobalConfig("/home/orange/tinytiny-rpc/tinytinyRPC/conf/rocket.xml");
  rocket::Logger::InitGlobalLogger(0);

  std::shared_ptr<OrderImp> service = std::make_shared<OrderImp>();
  // 注册rpc_service
  rocket::RPCDispatcher::GetRPCDispatcher()->registerService(service);

  test_tcp_server();
}